<!-- 商品详情-底部按钮列表 -->
<template>
    <el-collapse modelValue="1">
        <el-collapse-item title="底部按钮设置" name="1">
            <template v-if="defaultList.length > 0">
                <el-dropdown trigger="click" class="add-btn-dropdown">
                    <el-button
                        type="primary"
                        size="large"
                        class="add-btn"
                    >
                        添加按钮
                    </el-button>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <template v-for="(item, index) in defaultList" :key="index">
                                <el-dropdown-item @click.stop="onAddItem(item)">
                                    {{ item.label }}
                                </el-dropdown-item>
                            </template>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
            </template>

            <draggable
                v-model="list"
                group="goodsDetailBottomBtns"
                :animation="200"
                :item-key="getKey"
                handle=".draggableHandle"
            >
                <template #item="{ element, index }">
                    <div class="row-wrap">
                        <div class="left-wrap">
                            {{ element.label }}
                        </div>
                        <div class="right-wrap">
                            <el-icon title="拖动排序" class="sort-btn draggableHandle"><icon-sort /></el-icon>
                            <el-icon title="删除" class="delete-btn" @click.stop="list.splice(index, 1)"><icon-delete /></el-icon>
                        </div>
                    </div>
                </template>
            </draggable>
        </el-collapse-item>
    </el-collapse>
</template>

<script>
import { defineComponent, toRefs, computed } from 'vue'
import { randomStr } from '@/libs/tools'
export default defineComponent({
    name: 'GoodsDetailBottomBtns',
    props: {
        element: {
            type: Object,
            default: () => ({})
        }
    },
    setup(props) {
        const { element } = toRefs(props)

        const list = computed({
            get: () => element.value.customFeature.goodsBottomBtns,
            set: val => element.value.customFeature.goodsBottomBtns = val
        })

        const defaultList = computed(() => [
            { label: '首页', name: 'home' },
            { label: '客服', name: 'contact' },
            { label: '购物车', name: 'shopCart' },
            { label: '分享', name: 'share' }
        ].filter(item => !list.value.some(n => n.name === item.name)))

        const getKey = () => randomStr(12)

        const onAddItem = data => {
            list.value.push(data)
        }

        return {
            element,

            list,
            defaultList,

            getKey,
            onAddItem
        }
    }
})
</script>

<style lang="less" scoped>

.add-btn-dropdown {
    width: 100%;
    margin-bottom: 20px;
    ::v-deep(.el-tooltip__trigger) {
        width: 100%;
    }
}
.add-btn {
    width: 100%;
    
}

.row-wrap {
    width: 100%;
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 15px;
    box-shadow: 0 0 4px rgb(0 0 0 / 10%);
    border: 1px solid var(--edit-border-color);
    box-sizing: border-box;
    & + .row-wrap {
        margin-top: 10px;
    }
    .left-wrap {
        // 
    }
    .right-wrap {
        display: flex;
        align-items: center;
        .el-icon {
            font-size: 16px;
            & + .el-icon {
                margin-left: 10px;
            }
        }
        .sort-btn {
            cursor: move;
        }
        .delete-btn {
            cursor: pointer;
        }
    }
}
</style>